package com.qf.handler.in;

import com.qf.entity.Msg;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 添加了一个bug
 */
@Component
@ChannelHandler.Sharable
@Order(2000)
public class HeartChannelHandler extends SimpleChannelInboundHandler<Msg> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Msg msg) throws Exception {
        if (msg.getType() == 0) {
            //心跳消息
            System.out.println("接收到客户端的心跳消息....");
            ctx.writeAndFlush(msg);
        } else {
            ctx.fireChannelRead(msg);
        }
    }
}

//@ChannelHandler.Sharable
//abstract class BaseChannelHandler extends SimpleChannelInboundHandler<Msg> {
//
//    @Override
//    protected void channelRead0(ChannelHandlerContext ctx, Msg msg) throws Exception {
//        if (msg.getType() == msgType()) {
//            msgHandler(msg);
//        } else {
//            ctx.fireChannelRead(msg);
//        }
//    }
//
//    public abstract int msgType();
//
//    public abstract void msgHandler(Msg msg);
//}
